package com.example.administrator.exkotlin.net

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_net.*
import java.lang.StringBuilder

class NetActivity : AppCompatActivity() {
    val TAG = "NetActivity"
    private val url = "http://www.kuaidi100.com/query?type=yuantong&postid=11111111111"
    private val mUrlResponseListtner = UrlResponseListtner()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_net)
        initData()
    }

    private fun initData() {
        btn_httpUrlCon.setOnClickListener {
            refreshTv()
            HttpURLRequstIml().excute(mUrlResponseListtner, url)
        }
        btn_okhttp.setOnClickListener {
            refreshTv()
            OkHttpIml().excute(mUrlResponseListtner, url)
        }
        btn_retrofit.setOnClickListener {
            refreshTv()
            RetrofitIml().excute(mUrlResponseListtner,"")
        }
    }

    private fun refreshTv() {
        //确保在主线程运行
        runOnUiThread {
            tv_result.text = ""
        }
    }

    inner class UrlResponseListtner : IHttpRqust.URlListener {
        override fun <T> response(result: T) {
            val info = result as String
            Log.i(TAG, info)
            runOnUiThread {
                tv_result.text = info

            }
        }

    }
}
